package com.controller;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

//不要忘记写@WebServlet注释
@WebServlet("/Hi")
public class HelloServlet extends HttpServlet {

	//重写doGet方法
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.doPost(req, resp);
	}

	//重写doPost方法
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置请求的编码格式 utf-8
		req.setCharacterEncoding("utf-8");
		//设置响应的内容类型 text/html;charset=utf-8
	    resp.setContentType("text/html;charset=utf-8");
	    //获取传递过来的参数username
	    String name = req.getParameter("username");
	    //获取传递过来的参数pwd
	    String password = req.getParameter("pwd");
	    //如果name为tiger并且password为123,则跳转到路径为Success的Servlet页面
	    if(name.equals("tiger")&&password.equals("123")) {
	    	req.getRequestDispatcher("Success").forward(req, resp);
	    }else {
	    //如果用户名或者密码有误,则跳转到路径为Fail的Servlet页面
	    	req.getRequestDispatcher("Fail").forward(req, resp);
	    }
	   
	}
		
}
